package com.activiti.demo.demos.flow.controller;

import com.activiti.demo.demos.flow.component.ActComponent;
import com.activiti.demo.demos.flow.dto.VariablesDTO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@AllArgsConstructor
public class ActController {

    private final ActComponent actComponent;

//    private final FlowImageComponent imageComponent;

    /**
     * 部署流程
     */
    @PostMapping("/deploy")
    public void deploy() {
        // 读取bpmn文件流
        InputStream bpmnStream = getClass().getResourceAsStream("/process/请假流程4.bpmn20.xml");
        log.info("deploy接口返回:{}", actComponent.deploy(bpmnStream));
    }

    /**
     * 发起流程
     *
     * @param deployId 流程部署ID
     */
    @PostMapping("/start")
    public void start(@RequestParam("deployId") String deployId, @RequestParam("name") String name) {
        log.info("start接口返回:{}", actComponent.startProcess(deployId, name));
    }

    /**
     * 查看流程当前未完成节点
     *
     * @param processId 流程实例ID
     */
    @GetMapping("/unfinished/{processId}")
    public void unfinished(@PathVariable("processId") String processId) {
        actComponent.getUnfinishedNodes(processId);
    }

    /**
     * 提交任务
     *
     * @param paramDTO 任务参数DTO
     */
    @PostMapping("/complete")
    public void complete(@RequestBody VariablesDTO paramDTO) {
        log.info("提交参数如下:{}", paramDTO.toString());
        Map<String, Object> variables = new HashMap<>();
        //form转map
        paramDTO.getValues().forEach(value -> variables.put(value.getKey(), value.getValue()));
        actComponent.completeTask(paramDTO.getProcessId(), paramDTO.getUser(), variables);
    }

    /**
     * 获取流程实例图base64
     * todo:有问题 输出的图片不是流程实例图
     * @param processId 流程实例ID
     */
    @GetMapping("/image/{processId}")
    public String image(@PathVariable("processId") String processId) throws Exception {
        return actComponent.getProcessDiagramBas64(processId);
    }
}
